package com.jiguang.property.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiguang.property.dto.BuildInfoDTO;
import com.jiguang.property.dto.ParkInfoDTO;
import com.jiguang.property.entity.BuildInfo;
import com.jiguang.property.entity.CostInfo;
import com.jiguang.property.entity.ParkInfo;
import com.jiguang.property.mapper.ParkInfoMapper;
import com.jiguang.property.service.CostInfoService;
import com.jiguang.property.service.ParkInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiguang.property.utils.Result;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2025-03-23
 */
@Service
public class ParkInfoServiceImpl extends ServiceImpl<ParkInfoMapper, ParkInfo> implements ParkInfoService {
    @Autowired
    private CostInfoService costInfoService;

    @Override
    public Result listParkInfo(ParkInfoDTO parkInfoDTO) {
        //创建分页对象
        Page<ParkInfo> page = new Page<>(parkInfoDTO.getPageNow(),parkInfoDTO.getPageSize());

        QueryWrapper<ParkInfo> wrapper = new QueryWrapper<>();
        //判断楼宇信息是否为空
        if (!StringUtil.isNullOrEmpty(parkInfoDTO.getLocation())){
            wrapper.like("location",parkInfoDTO.getLocation());
        }
        wrapper.eq("is_delete",0);
        wrapper.orderByDesc("update_time");

        Page<ParkInfo> buildInfoPage = (Page<ParkInfo>) baseMapper.selectPage(page,wrapper);


        return Result.ok().message("查询成功").data("parkInfos", buildInfoPage.getRecords()).data("total",buildInfoPage.getTotal());
    }

    @Override
    public Result deleteParkInfo(String parkIds) {
        if (StringUtils.isEmpty(parkIds)) return Result.error().message("Id不能为空");
        List<String> ids = Arrays.asList(parkIds.split(","));

        //查询是否欠费
        for (String id : ids) {
            ParkInfo parkInfo = baseMapper.selectById(id);
            QueryWrapper<CostInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("type","物业费");
            wrapper.eq("user_id",parkInfo.getUserId());
            int count = costInfoService.count(wrapper);
            if(count >0){
                return Result.error().message("物业费欠缴，无法删除");
            }
        }

        baseMapper.deleteBatchIds(ids);
        return Result.ok().message("删除成功");
    }

    @Override
    public Result updateOrAddParkInfo(ParkInfoDTO parkInfoDTO) {
        ParkInfo parkInfo = new ParkInfo();
        BeanUtils.copyProperties(parkInfoDTO,parkInfo);

        if (parkInfoDTO.getStatus() == 1 && StringUtils.isEmpty(parkInfoDTO.getUserId())){
            return Result.error().message("请选择业主");
        }else if(parkInfoDTO.getStatus() == 0){
            parkInfo.setUserId("");
        }

        if (StringUtil.isNullOrEmpty(parkInfo.getId())){
            boolean b = existPark(parkInfoDTO);
            if (b) {return Result.error().message("该车位已存在");}
            baseMapper.insert(parkInfo);
        }else{
            boolean b = existPark(parkInfoDTO);
            if (b) {return Result.error().message("该车位已存在");}
            baseMapper.updateById(parkInfo);
        }
        return Result.ok().message("成功");
    }

    @Override
    public Result getPark() {
        List<ParkInfo> parkInfos = baseMapper.selectList(null);
        int uSole = 0;
        int nSole = 0;
        for (ParkInfo parkInfo : parkInfos) {
            if(parkInfo.getStatus() == 0){
                nSole = nSole + 1;
            }else{
                uSole = uSole + 1;
            }
        }

        return Result.ok().data("uSole",uSole).data("nSole",nSole);
    }

    /**
     * 判断是否存在该车位
     */
    private boolean existPark(ParkInfoDTO parkInfoDTO){
        QueryWrapper<ParkInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("location",parkInfoDTO.getLocation());
        wrapper.eq("parking_number",parkInfoDTO.getParkingNumber());
        ParkInfo parkInfo = baseMapper.selectOne(wrapper);

        if (parkInfo != null){
            if (!StringUtils.isEmpty(parkInfo.getId()) && !parkInfo.getId().equals(parkInfoDTO.getId())){
                return true;
            }else if (StringUtils.isEmpty(parkInfoDTO.getId())){
                return true;
            }else{
                return false;
            }
        }

        return false;
    }
}
